

import { IParam } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
// import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import { refsystemid, systemid } from '../../atom/config';
import get_data from '../../atom/get-data';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
import P01 from './P01';
import P03 from './P03';
import P04 from './P04';
import P05 from './P05';
function chunk(arr: Array<{}>, size: number) {
	const arr2 = new Array();
	for (let i = 0; i < arr.length; i = i + size) {
		const arr1 = arr;
		arr2.push(arr1.slice(i, i + size));
	}
	return arr2;
}
interface Aa {
	field: string;
}
interface Vv {
	w: string;
	v: string;
	arr: object[];

}
export default async function a015(fd: IFeidaoAiBrowserComponent) {
	//  调用事件
	const role_type_id = get(fd, 'role_type_id');

	const productid = get(fd, 'productid');
	const param = get(fd, 'criteria') as IParam;
	const index = get(fd, 'page_no') as number;
	show_loading();
	const res = await nodejs<{ length: number }>("roler/zj-000009/s003", { productid, role_type_id }) as Array<{}>;
	hide_loading();
	if (res.length === 0) {
		render(fd, '', P01, 'P01', 'inner');
	} else {
		render(fd, res, P01, 'P01', 'inner');
	}
	// const sort = get(fd, 'sort') as ISort;
	// const sort = {};
	const arr = res.map((a: Aa) => {
		return a.field;
	});
	show_loading();
	const res1 = await nodejs<{ length: number; }>("roler/zj-000009/s004", { productid, role_type_id });
	hide_loading();
	if (res1.length === 0) {

	} else {
		// const aa = await get_data(res1[0].systemid, null, res1[0].modelid, res1[0].tablename, {
		// 	paging: {
		// 		index: 1,
		// 		size: 0
		// 	},
		// 	param,
		// 	sort
		// });
		const aa = await get_data(systemid, refsystemid, res1[0].modelid, res1[0].tablename, { paging: { index: 1, size: 0 }, param }) as Vv[];
		if (aa.length === 0) {
			render(fd, '', P03, 'P03', 'inner');
			render(fd, '', P04, 'P04', 'inner');
			fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P05"]').innerHTML = '';
		} else {
			fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P04"]').innerHTML = '';
			const data = aa.map((a) => {
				const obj = {} as { v: string; arr: string[] };
				arr.map((v) => {
					obj[v] = a[v];
				});
				obj.v = JSON.stringify(obj);
				obj.arr = arr;
				return obj;
			});
			const dataArray = chunk(data, 10);
			const fenye = {
				count: data.length,
				page_no: index,
				page_size: 10
			};
			render(fd, fenye, P05, 'P05', 'inner');
			render(fd, dataArray[index - 1], P03, 'P03', 'inner');
		}
	}
}
